<?php
/**
* @version		$Id: helper.php 
* @package		Joomla
* @copyright	Copyright (C) 2012. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


class modBackRotatorHelper
{
    
       
	function getSetShowThumbs(&$params){
            
            return $params->get('setShowThumbs','no');

        }
	function getWidthForDivsThumbs(&$params){
            
            return $params->get('widthDivsThumbs');

        }
	function getHeigthForDivsThumbs(&$params){
            
            return $params->get('heigthDivsThumbs');

        }
	function getStyleForThumbs(&$params){
            
            return $params->get('style',false);

        }
	function getAnimationForThumbs(&$params){
            
            return $params->get('animation',false);

        }
        /**
         *funzioni per ricavare i 5 file specificati 
         * con relativo nome e link 
         */
	function getImgLink(&$params){
            $arrayParams = array();
            //aggiungere  controllo 
            for($i=0 ,$w=1; $i<=5 ; $i++, $w++){
                $arrayParams [$i]["img"] = $params-> get($w.'img');
                $arrayParams [$i]["href"] = $params-> get($w.'href');
            }
            return $arrayParams;

        }
        /**
         *qui immagini e link vengono ricavati da un file 
         * editato dall'utonto
         *  
         */
        function getImgLinkFromFile(&$params,$imgFolder,$JSONfolder){
            $arrayToReturn = array();
            $JSONfolder = JPATH_BASE.DS.$JSONfolder;
            
//            if (!file_exists($JSONfolder)) die ('file non trovato');
            $string = file_get_contents($JSONfolder);
            $json_a = json_decode($string, true);
            
            $i=0;
            foreach ($json_a as $key => $value) {
                                   
                    foreach ($value as $indexRow => $valueRow) {
                        $arrayToReturn[$i]['nameImg']=$imgFolder.$valueRow['nameImg'];
                        $arrayToReturn[$i]['linkImage']=$valueRow['linkImage'];
                        $i++;
                    }
                
            }
            
          
            
            return $arrayToReturn;

        }
        
	function getTime(&$params){
            return                 $time = $params-> get('time','5000');

        }
	function getDiv(&$params){
            return                 $divToCustomize = $params-> get('div','body');

        }
	function getwidthThumb(&$params){
            return                 $widthForThumbs = $params-> get('widthThumb');

        }
	function getheigthThumb(&$params){
            return                 $heigthForThumbs = $params-> get('heightThumb');

        }
	function getImages(&$params, $folder){
                
                
            
		$type 		= $params->get( 'type', 'jpg' );

		$files	= array();
		$images	= array();

		$dir = JPATH_BASE.DS.$folder;

		// check if directory exists
		if (is_dir($dir))
		{
			if ($handle = opendir($dir)) {
				while (false !== ($file = readdir($handle))) {
					if ($file != '.' && $file != '..' && $file != 'CVS' && $file != 'index.html' ) {
						$files[] = $file;
					}
				}
			}
			closedir($handle);

			$i = 0;
			foreach ($files as $img)
			{
				if (!is_dir($dir .DS. $img))
				{
					if (preg_match("#$type#i", $img)) {
						$images[$i]->name 	= $img;
						$images[$i]->folder	= $folder;
						++$i;
					}
				}
			}
		}

		return $images;
	}

	function getFolder(&$params,$kindFolder)
	{
		$folder 	= $params->get( $kindFolder );

		$LiveSite 	= JURI::base();

		// if folder includes livesite info, remove
		if ( JString::strpos($folder, $LiveSite) === 0 ) {
			$folder = str_replace( $LiveSite, '', $folder );
		}
		// if folder includes absolute path, remove
		if ( JString::strpos($folder, JPATH_SITE) === 0 ) {
			$folder= str_replace( JPATH_BASE, '', $folder );
		}
		$folder = str_replace('\\',DS,$folder);
		$folder = str_replace('/',DS,$folder);
//                $folder = JPATH_BASE.DS.$folder;
		return $folder;
	}
	
}

//function getRandomImage(&$params, $images)
//	{
//		$width 		= $params->get( 'width' );
//		$height 	= $params->get( 'height' );
//
//		$i 				= count($images);
//		$random 		= mt_rand(0, $i - 1);
//		$image 			= $images[$random];
//		$size 			= getimagesize (JPATH_BASE.DS.$image->folder .DS. $image->name);
//
//
//		if ($width == '') {
//			$width = 100;
//		}
//
//		if ($size[0] < $width) {
//			$width = $size[0];
//		}
//
//		$coeff = $size[0]/$size[1];
//		if ($height == '') {
//			$height = (int) ($width/$coeff);
//		} else {
//			$newheight = min ($height, (int) ($width/$coeff));
//			if ($newheight < $height) {
//				$height = $newheight;
//			} else {
//				$width = $height * $coeff;
//			}
//		}
//
//		$image->width 	= $width;
//		$image->height	= $height;
//		$image->folder	= str_replace( '\\', '/', $image->folder );
//
//		return $image;
//	}